package com.hu.controller;


import com.hu.bookstorecommon.entity.user;
import com.hu.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller

public class userController {
    @Autowired
    private userService userService;


    @RequestMapping("/user_login")
    @ResponseBody
    public Map<String, Object> login(String username, String password, HttpSession session) {

        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());

        Map<String, Object> result = new HashMap<>();
        user user = userService.login(username);
        if (user == null) {
            result.put("success", -1);
            result.put("message", "用户名不存在");
        } else if (!pwd.equals(user.getPassword())) {
            result.put("success", -2);
            result.put("message", "密码错误");
        } else {
            result.put("success", 0);
            result.put("message", user.getName());

            session.setAttribute("user", user);
        }

        return result;
    }

    @RequestMapping("/user_register")
    @ResponseBody
    public Map<String, Object> userRegister(user user) {
        Map<String, Object> result = new HashMap<>();
        //System.out.println(user.getUsername()+user.getPassword()+user.getName()+user.getPhone());

        if (user.getUsername() == null || user.getUsername().equals("")) {

            result.put("success", -1);
            result.put("message", "登录名不能为空");
            return result;
        }
        user tmpUser = userService.login(user.getUsername());
        if (tmpUser != null) {

            result.put("success", -1);
            result.put("message", "登录名已存在");
            return result;

        }
        String password = user.getPassword();
        if (password == null || password.equals("")) {
            result.put("success", -2);
            result.put("message", "密码不能为空");

            return result;
        }
        if (password.length() < 6) {
            result.put("success", -2);
            result.put("message", "密码过短，至少六位密码");

            return result;
        }

        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(pwd);


        String name = user.getName();
        if (name == null || name.equals("")) {
            result.put("success", -3);
            result.put("message", "姓名不能为空");

            return result;
        }
        //判断手机
        String phone = user.getPhone();
        if (phone == null || !phone.matches("^1\\d{10}")) {
            result.put("success", -4);
            result.put("message", "手机号为空或不符合格式");

            return result;
        }
        //添加用户
        if (userService.userRegister(user) > 0) {
            result.put("success", 1);
            result.put("message", "注册成功，请登录系统");
        }
        else {
            result.put("success", 0);
            result.put("message", "系统繁忙，请稍候再试");
        }

        return result;
    }
    }




